﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DBConnection
{
    public class ChannelsDAO
    {
        public static List<String> GetAllChannels(TwitterDataContext context)
        {
            List<string> channels =
                (from c in context.Channels
                 select c.Name
                ).ToList();
            return channels;
        }

        public static bool RegisterChannel(string name, string ownerName,
            TwitterDataContext context)
        {
            if (GetChannelID(name, context) != null)
            {
                return false;
            }

            int ownerId = (int)UsersDAO.GetUserID(name, context);

            Channel newChannel = new Channel();
            newChannel.Name = name;
            newChannel.OwnerID = ownerId;
            context.Channels.InsertOnSubmit(newChannel);
            context.SubmitChanges();
            return true;
        }

        public static int? GetChannelID(string name, TwitterDataContext context)
        {
            var channel =
                from c in context.Channels
                where c.Name == name
                select c.ChannelID;

            if (channel.ToList().Count == 0)
            {
                return null;
            }
            return channel.ToList()[0];
        }
    }
}
